package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ISocPlanFeedbackRecordDAO;
import org.jsola.hr.dto.EmpSocPlanAttachDTO;
import org.jsola.hr.dto.SocPlanFeedbackRecordAddDTO;
import org.jsola.hr.dto.SocPlanFeedbackRecordUpdateDTO;
import org.jsola.hr.entity.SocPlanFeedbackRecordDO;
import org.jsola.hr.provider.IUserProviderService;
import org.jsola.hr.query.SocPlanFeedbackRecordQuery;
import org.jsola.hr.service.ISocPlanFeedbackRecordService;
import org.jsola.hr.vo.SocPlanFeedbackRecordListVO;
import org.jsola.hr.vo.SocPlanFeedbackRecordVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 增减员计划反馈记录表
 *
 * @author zhr
 */
@Service("hrSocPlanFeedbackRecordServiceImpl")
public class SocPlanFeedbackRecordServiceImpl implements ISocPlanFeedbackRecordService {

    @Autowired
    private ISocPlanFeedbackRecordDAO socPlanFeedbackRecordDAO;

    @Autowired
    private IUserProviderService userProviderService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SocPlanFeedbackRecordVO save(SocPlanFeedbackRecordAddDTO socPlanFeedbackRecordAddDTO, TokenUser tokenUser) {
        // 转DO
        SocPlanFeedbackRecordDO socPlanFeedbackRecordDO = socPlanFeedbackRecordAddDTO.to(SocPlanFeedbackRecordDO.class);
        // 保存
        socPlanFeedbackRecordDO = save(socPlanFeedbackRecordDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return socPlanFeedbackRecordDO.to(SocPlanFeedbackRecordVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(SocPlanFeedbackRecordUpdateDTO socPlanFeedbackRecordUpdateDTO, TokenUser tokenUser) {
        //转DO
        SocPlanFeedbackRecordDO socPlanFeedbackRecordDO = socPlanFeedbackRecordUpdateDTO.to(SocPlanFeedbackRecordDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(socPlanFeedbackRecordDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... socPlanFeedbackRecordIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) socPlanFeedbackRecordIds);
    }


    @Override
    public SocPlanFeedbackRecordVO selectById(String socPlanFeedbackRecordId, String siteId) {
        SocPlanFeedbackRecordDO socPlanFeedbackRecordDO = selectDOById(socPlanFeedbackRecordId, siteId);
        if (socPlanFeedbackRecordDO == null) {
            return null;
        }
        return socPlanFeedbackRecordDO.to(SocPlanFeedbackRecordVO.class);
    }

    @Override
    public List<SocPlanFeedbackRecordListVO> select(SocPlanFeedbackRecordQuery socPlanFeedbackRecordQuery, String siteId) {
        List<SocPlanFeedbackRecordDO> socPlanFeedbackRecordDOList = selectDO(socPlanFeedbackRecordQuery, siteId);
        if (CollectionUtils.isEmpty(socPlanFeedbackRecordDOList)) {
            return socPlanFeedbackRecordDOList == null ? null : new ArrayList<>();
        }
        return socPlanFeedbackRecordDOList.stream()
                .map(socPlanFeedbackRecordDO -> socPlanFeedbackRecordDO.to(SocPlanFeedbackRecordListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(SocPlanFeedbackRecordQuery socPlanFeedbackRecordQuery, String siteId) {
        Example example = buildExample(socPlanFeedbackRecordQuery, siteId);
        return socPlanFeedbackRecordDAO.selectCountByExample(example);
    }

    @Override
    public Page<SocPlanFeedbackRecordListVO> selectPage(SocPlanFeedbackRecordQuery socPlanFeedbackRecordQuery, String siteId) {
        Example example = buildExample(socPlanFeedbackRecordQuery, siteId);
        Page<SocPlanFeedbackRecordDO> page = socPlanFeedbackRecordDAO.selectPageByExample(example,
                socPlanFeedbackRecordQuery.getPageNo(),
                socPlanFeedbackRecordQuery.getPageSize());

        return page.to(SocPlanFeedbackRecordListVO.class);
    }

    @Override
    public SocPlanFeedbackRecordDO selectDOById(String socPlanFeedbackRecordId, String siteId) {
        return listById(socPlanFeedbackRecordId, siteId);
    }

    @Override
    public List<SocPlanFeedbackRecordDO> selectDO(SocPlanFeedbackRecordQuery socPlanFeedbackRecordQuery, String siteId) {
        Example example = buildExample(socPlanFeedbackRecordQuery, siteId);
        return socPlanFeedbackRecordDAO.selectByExample(example);
    }

    @Override
    public List<SocPlanFeedbackRecordVO> selectRecordByPlanId(String planId, String siteId) {
        Example example = new Example(SocPlanFeedbackRecordDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId)
                .andEqualTo("planId", planId);
        example.orderBy("gmtCreate").desc();
        List<SocPlanFeedbackRecordDO> recordList = socPlanFeedbackRecordDAO.selectByExample(example);
        return recordList.stream().map(record -> {
            SocPlanFeedbackRecordVO toResult = record.to(SocPlanFeedbackRecordVO.class);
            UserVO user = userProviderService.selectById(toResult.getUserId());
            if (!ObjectUtils.isEmpty(user)) {
                toResult.setUserName(user.getName());
            }
            return toResult;
        }).collect(Collectors.toList());
    }

    @Override
    public SocPlanFeedbackRecordDO initRecord(String feedbackState, String planId,
                                              EmpSocPlanAttachDTO planAttachDTO, TokenUser tokenUser) {
        SocPlanFeedbackRecordDO record = new SocPlanFeedbackRecordDO();
        record.setUserId(tokenUser.getUserId());
        record.setFeedbackState(feedbackState);
        record.setPlanId(planId);
        record.setFeedbackContent(planAttachDTO.getFeedbackContent());
        record.setAnnex(planAttachDTO.getAnnex());
        record.setSiteId(tokenUser.getSiteId());
        record.preInsert(tokenUser.getUserId());
        return record;
    }

    @Override
    @Async
    public void bachSave(List<SocPlanFeedbackRecordDO> saveList) {
        socPlanFeedbackRecordDAO.insertListAndSetId(saveList);
    }

    /**
     * 根据查询参数，构建example
     *
     * @param socPlanFeedbackRecordQuery 查询参数
     * @param siteId                     所属站点id
     * @return example
     */
    private Example buildExample(SocPlanFeedbackRecordQuery socPlanFeedbackRecordQuery, String siteId) {
        Example example = new Example(SocPlanFeedbackRecordDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example, socPlanFeedbackRecordQuery.getOrders());
        return example;
    }
}




